import { NGridItem, type GridItemProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export type GridItemFactoryConstructorType = {
  props?: GridItemProps;
  attrs?: HTMLAttributes;
  defaultSlot?: (data?: { overflow: boolean }) => BaseComponentType;
};

export class GridItemFactory {
  private props: GridItemProps = {};
  private attrs: HTMLAttributes = {};
  private defaultSlot: (data?: { overflow: boolean }) => BaseComponentType =
    () => null;

  constructor(data?: GridItemFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }
  setProps(props: GridItemProps) {
    this.props = props;
    return this;
  }
  setDefault(defaultSlot: (data?: { overflow: boolean }) => BaseComponentType) {
    this.defaultSlot = defaultSlot;
    return this;
  }

  create() {
    return (
      <NGridItem {...this.attrs} {...this.props}>
        {{
          default:
            this.defaultSlot() != null
              ? (data?: { overflow: boolean }) =>
                  getSlotsDom(this.defaultSlot(data))
              : null,
        }}
      </NGridItem>
    );
  }
}
